import requests
from bs4 import BeautifulSoup
def git_pinlun(url):
    from selenium import webdriver
    import time
    from selenium.webdriver.common.by import By
    driver = webdriver.Edge()
    driver.get(url)  # 替换为视频所在的网页地址
    time.sleep(1)
    element = driver.find_element(By.CSS_SELECTOR, "a.DouWeb-SR-subject-info-name") # 根据视频元素的ID进行查找
    href_value = element.get_attribute("href")
    return href_value[-8:]
def get_da(name):
    cookies = {
        'bid': 'bMCOc2NN9hE',
        'll': '"108303"',
        '_pk_id.100001.8cb4': '7b3a9e1b4eed7e8b.1703001784.',
        '_ga_PRH9EWN86K': 'GS1.2.1703002854.1.0.1703002854.0.0.0',
        '_ga': 'GA1.1.1068612282.1702965610',
        '_ga_Y4GN1R87RG': 'GS1.1.1703002868.1.0.1703002870.0.0.0',
        'dbcl2': '"276594306:m+mxAkyZNTM"',
        '__yadk_uid': 'MoWzIF77FPjuCUrE5EDKGs3khW4Zi9C4',
        'push_noty_num': '0',
        'push_doumail_num': '0',
        '__utmv': '30149280.27659',
        'ck': '6aoA',
        '_pk_ref.100001.8cb4': '%5B%22%22%2C%22%22%2C1703162361%2C%22https%3A%2F%2Fcn.bing.com%2F%22%5D',
        '_pk_ses.100001.8cb4': '1',
        'ap_v': '0,6.0',
        '__utma': '30149280.1068612282.1702965610.1703061088.1703162363.4',
        '__utmc': '30149280',
        '__utmz': '30149280.1703162363.4.2.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/',
        '__utmt': '1',
        '__utmb': '30149280.8.10.1703162363',
        'frodotk_db': '"c8ed5c0f2701c91d5ac8142d75055b57"',
    }

    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        # 'Cookie': 'bid=bMCOc2NN9hE; ll="108303"; _pk_id.100001.8cb4=7b3a9e1b4eed7e8b.1703001784.; _ga_PRH9EWN86K=GS1.2.1703002854.1.0.1703002854.0.0.0; _ga=GA1.1.1068612282.1702965610; _ga_Y4GN1R87RG=GS1.1.1703002868.1.0.1703002870.0.0.0; dbcl2="276594306:m+mxAkyZNTM"; __yadk_uid=MoWzIF77FPjuCUrE5EDKGs3khW4Zi9C4; push_noty_num=0; push_doumail_num=0; __utmv=30149280.27659; ck=6aoA; _pk_ref.100001.8cb4=%5B%22%22%2C%22%22%2C1703162361%2C%22https%3A%2F%2Fcn.bing.com%2F%22%5D; _pk_ses.100001.8cb4=1; ap_v=0,6.0; __utma=30149280.1068612282.1702965610.1703061088.1703162363.4; __utmc=30149280; __utmz=30149280.1703162363.4.2.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utmt=1; __utmb=30149280.8.10.1703162363; frodotk_db="c8ed5c0f2701c91d5ac8142d75055b57"',
        'Referer': 'https://www.douban.com/',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-User': '?1',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
        'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
    }

    params = {
        'source': 'suggest',
        'q': {name},
    }
    return cookies,headers,params
if __name__ == '__main__':
    name = input("请输入你要爬的影评")
    cookies ,headers,params= get_da(name)
    response = requests.get('https://www.douban.com/search', params=params, cookies=cookies, headers=headers)
    print(response.url)
    b=git_pinlun(response.url)
    page = 2
    while True:
        page_url = f'https://movie.douban.com/subject/{b}/comments?start={page*20}&limit=20&status=P&sort=new_score'
        response = requests.get(page_url,cookies=cookies,headers=headers)
        if response.status_code == 200:
            soup = BeautifulSoup(response.content, 'html.parser')
            comments = soup.find_all('p', class_="comment-content")
            file = open('666.txt', mode='a',encoding='utf-8')
            for comment in comments:
                text = comment.span.text.strip()
                file.write(text)
                print(text)
            file.close()
            page = page + 1
        else:
            break
